import store from "@/store";
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path: "/goods",
    name: "goods",
    component: () => import("../views/Goods.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import("../views/User.vue"),
    redirect: "/user/cart",
    children: [
      { path: "cart", component: () => import("../components/user/Cart.vue") },
    ],

    //  路由独享守卫
    /*     beforeEnter: (to, from, next) => {
      console.log("路由独享守卫..");
      let token = localStorage.getItem("x-auth-token");
      if (token) {
        next();
      } else {
        // alert("请先登录");
        store.dispatch("showToast/asyncIsShowToast", {
          isShow: true,
          title: "请先登录",
          type: "warning",
        });
      }
    }, */
    //  ...................独享守卫
  },
  {
    path: "/order",
    name: "order",
    component: () => import("../views/Order.vue"),
  },
  {
    path: "/free",
    name: "free",
    component: () => import("../views/Free.vue"),
  },
  {
    path: "/details",
    name: "details",
    component: () => import("../views/Details.vue"),
  },
  {
    path: "*",
    component: () => import("../views/Error.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

/* // 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log("路由前置守卫", to, from);
  if (to.path == "/user") {
    let token = localStorage.getItem("x-auth-token");
    if (token) {
      next();
    } else {
      store.dispatch("showToast/asyncIsShowToast", {
        isShow: true,
        title: "请先登录",
        type: "warning",
      });
    }
  } else {
    next();
  }
}); */

export default router;
